package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author chencfgs
 * @date 2019-12-04 9:30
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


//    @Override
//    public void add(List<OrderSetting> data) {
//        if (data != null && data.size() > 0) {
//            for (OrderSetting setting : data) {
//                long countByOrderDate = orderSettingDao.findCountByOrderDate(setting.getOrderDate());//
//                if (countByOrderDate > 0) {
//                    //已经进行了预约设置，执行更新操作
//                    orderSettingDao.editNumberByOrderDate(setting);
//                } else {
//                    //没有进行了预约设置，执行更新操作
//                    orderSettingDao.add(setting);
//                }
//            }
//        }
//    }

    @Override
    public void add(List<OrderSetting> data) {
        if (data != null && data.size() > 0) {
            for (OrderSetting orderSetting : data) {
                Long aLong = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (aLong > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOderSettingByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";

        HashMap map = new HashMap();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map orderSettingMap = new HashMap();
                orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//日期
                orderSettingMap.put("number", orderSetting.getNumber());//预约人数
                orderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
                data.add(orderSettingMap);
            }
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (countByOrderDate > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public OrderSetting queryByDate(Date date) {
        return orderSettingDao.queryByDate(date);
    }

    @Override
    public void addReservations(OrderSetting orderSetting) {
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
    }
}
